package red.action;


import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import red.service.impl.pagingServiceImpl;
import red.util.BaseAction;
import red.util.PageHelper;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
public class PageAction extends BaseAction{

    private Map<String,Object> result;
    List list=new ArrayList<>();
    private int Total;
    private int name;

    public Map<String, Object> getResult() {
        return result;
    }

    public void setResult(Map<String, Object> result) {
        this.result = result;
    }


    //获取所有用户以及用户分页
    public String pagingUser(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String pageSize = req.getParameter("pageSize");
        String page = req.getParameter("page");
        if (page==null&&pageSize==null){
            result.put("data", null);
            result.put("msg", "查看失败");
        }else {
            if (page.length() == 0 && pageSize.length() == 0) {
                name = 1;
                pagingServiceImpl total = new pagingServiceImpl();
                Total = total.getTotal(name);
                pagingServiceImpl see = new pagingServiceImpl();
                list = see.allUser();
                PageHelper paging = new PageHelper(Total, page, pageSize, list);
                JSONObject object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("isFirstPage", paging.isFirstPage());
                    object.put("isLastPage", paging.isLastPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (list.size() > 0) {
                    result.put("data", object);
                    result.put("msg", "查看成功");

                } else {
                    result.put("data", null);
                    result.put("msg", "查看失败");
                }
            } else {
                if (page.length() != 0 && pageSize.length() != 0) {
                    name = 1;
                    pagingServiceImpl total = new pagingServiceImpl();
                    Total = total.getTotal(name);
                    pagingServiceImpl see = new pagingServiceImpl();
                    list = see.pageUser(pageSize, page);
                    PageHelper paging = new PageHelper(Total, page, pageSize, list);
                    JSONObject object = new JSONObject();
                    try {
                        object.put("Total", paging.getTotal());
                        object.put("Page", paging.getPage());
                        object.put("Pages", paging.getPages());
                        object.put("isFirstPage", paging.isFirstPage());
                        object.put("isLastPage", paging.isLastPage());
                        object.put("list", paging.getList());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (list.size() > 0) {
                        result.put("data", object);
                        result.put("msg", "查看成功");
                    } else {
                        result.put("data", null);
                        result.put("msg", "查看失败");
                    }
                } else {
                    result.put("data", null);
                    result.put("msg", "获取失败");
                }
            }
        }
        return "success";
    }


    //获取栏目以及栏目分页
    public String pagingColumn(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String pageSize = req.getParameter("pageSize");
        String page = req.getParameter("page");
        if (page==null&&pageSize==null){
            result.put("data", null);
            result.put("msg", "查看失败");
        }else {
            if (page.length() == 0 && pageSize.length() == 0) {
                name = 2;
                pagingServiceImpl total = new pagingServiceImpl();
                Total = total.getTotal(name);
                pagingServiceImpl see = new pagingServiceImpl();
                list = see.allColumn();
                PageHelper paging = new PageHelper(Total, page, pageSize, list);
                JSONObject object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("isFirstPage", paging.isFirstPage());
                    object.put("isLastPage", paging.isLastPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (list.size() > 0) {
                    result.put("data", object);
                    result.put("msg", "查看成功");

                } else {
                    result.put("data", null);
                    result.put("msg", "查看失败");
                }
            } else {
                if (page.length() != 0 && pageSize.length() != 0) {
                    name = 2;
                    pagingServiceImpl total = new pagingServiceImpl();
                    Total = total.getTotal(name);
                    pagingServiceImpl see = new pagingServiceImpl();
                    list = see.pageColumn(pageSize, page);
                    PageHelper paging = new PageHelper(Total, page, pageSize, list);
                    JSONObject object = new JSONObject();
                    try {
                        object.put("Total", paging.getTotal());
                        object.put("Page", paging.getPage());
                        object.put("Pages", paging.getPages());
                        object.put("isFirstPage", paging.isFirstPage());
                        object.put("isLastPage", paging.isLastPage());
                        object.put("list", paging.getList());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (list.size() > 0) {
                        result.put("data", object);
                        result.put("msg", "查看成功");
                    } else {
                        result.put("data", null);
                        result.put("msg", "查看失败");
                    }
                } else {
                    result.put("data", null);
                    result.put("msg", "获取失败");
                }
            }
        }
        return "success";
    }


    //获取文章以及文章分页
    public String pagingText(){
        result = new HashMap<String,Object>();
        //请求数据
        HttpServletRequest req = null;
        req = getRequest();
        String pageSize = req.getParameter("pageSize");
        String page = req.getParameter("page");
        if (page==null&&pageSize==null){
            result.put("data", null);
            result.put("msg", "查看失败");
        }else {
            if (page.length() == 0 && pageSize.length() == 0) {
                name = 3;
                pagingServiceImpl total = new pagingServiceImpl();
                Total = total.getTotal(name);
                pagingServiceImpl see = new pagingServiceImpl();
                list = see.allText();
                PageHelper paging = new PageHelper(Total, page, pageSize, list);
                JSONObject object = new JSONObject();
                try {
                    object.put("Total", paging.getTotal());
                    object.put("Page", paging.getPage());
                    object.put("Pages", paging.getPages());
                    object.put("isFirstPage", paging.isFirstPage());
                    object.put("isLastPage", paging.isLastPage());
                    object.put("list", paging.getList());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (list.size() > 0) {
                    result.put("data", object);
                    result.put("msg", "查看成功");

                } else {
                    result.put("data", null);
                    result.put("msg", "查看失败");
                }
            } else {
                if (page.length() != 0 && pageSize.length() != 0) {
                    name = 3;
                    pagingServiceImpl total = new pagingServiceImpl();
                    Total = total.getTotal(name);
                    pagingServiceImpl see = new pagingServiceImpl();
                    list = see.pageText(pageSize, page);
                    PageHelper paging = new PageHelper(Total, page, pageSize, list);
                    JSONObject object = new JSONObject();
                    try {
                        object.put("Total", paging.getTotal());
                        object.put("Page", paging.getPage());
                        object.put("Pages", paging.getPages());
                        object.put("isFirstPage", paging.isFirstPage());
                        object.put("isLastPage", paging.isLastPage());
                        object.put("list", paging.getList());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if (list.size() > 0) {
                        result.put("data", object);
                        result.put("msg", "查看成功");
                    } else {
                        result.put("data", null);
                        result.put("msg", "查看失败");
                    }
                } else {
                    result.put("data", null);
                    result.put("msg", "获取失败");
                }
            }
        }
        return "success";
    }


    public static HttpServletRequest getRequest(){
        return (HttpServletRequest) ServletActionContext.getRequest();
    }
}
